<?php

namespace common\components;

require_once __DIR__ . "/weixin/WxPay.Api.php";
require_once __DIR__ . "/weixin/WxPay.JsApiPay.php";
use common\models\Wechat;
use Yii;
use yii\log\Logger;

/**
 * Class WeixinPay
 * @package common\components
 */
class WeixinPay
{
    /**
     * 退款申请
     * @param $orderId  订单id
     * @param $payment  支付方式
     * @param $deposit  退款金额
     */
    public static function refund($orderId, $wechatId, $deposit)
    {
        $input = new \WxPayRefund();
        $wechat = Wechat::findOne($wechatId);
        //证书路径
        self::wechatParams($wechat);
        $input->SetAppid($wechat->appid);
        $input->SetMch_id($wechat->wx_mchid);
        $input->SetOut_trade_no($orderId);
        $input->SetOut_refund_no($orderId);
        $input->SetTotal_fee($deposit);
        $input->SetRefund_fee($deposit);
        $input->SetOp_user_id($wechat->wx_mchid);

        $refund = \WxPayApi::refund($input);
        Yii::getLogger()->log('Weixinpay refund created, response from weixin:' . json_encode($refund),
            Logger::LEVEL_INFO);

        if (isset($refund['result_code']) && $refund['result_code'] === 'SUCCESS') {
            return ['success' => true, 'data' => 'success'];
        }
        return ['success' => false, 'message' => isset($refund['err_code_des']) ? $refund['err_code_des'] : $refund['return_msg']];
    }

    /**
     * 退款查询
     * @param $orderId
     * @param $payment
     * @return \成功时返回
     * @throws \WxPayException
     */
    public static function refundQuery($orderId, $payment)
    {
        $input = new \WxPayRefundQuery();
        $input->SetAppid(Yii::$app->params['weixin.appId']);
        $input->SetMch_id(Yii::$app->params['weixin.mchId']);
        $input->SetOut_refund_no($orderId);
        $refundQuery = \WxPayApi::refundQuery($input);
        Yii::getLogger()->log('Weixinpay refund query, response from weixin:' . json_encode($refundQuery),
            Logger::LEVEL_INFO);
        return (isset($refundQuery['result_code']) && $refundQuery['result_code'] === 'SUCCESS')
            ? ['success' => true, 'data' => isset($refundQuery['refund_status_0']) ? $refundQuery['refund_status_0'] : '']
            : ['success' => false, isset($refundQuery['err_code_des']) ? $refundQuery['err_code_des']
                : $refundQuery['return_msg']];
    }
    public static function wechatParams($wechat){
        \WxPayApi::$appid = $wechat->appid;
        \WxPayApi:: $appsecret = $wechat->appsecret;
        \WxPayApi:: $mchid = $wechat->wx_mchid;
        \WxPayApi:: $sslcertPath = __DIR__ . '/weixin/cert/apiclient_'.$wechat->appid.'_cert.pem';
        \WxPayApi:: $sslkeyPath = __DIR__ . '/weixin/cert/apiclient_'.$wechat->appid.'_key.pem';
    }

    /**
     * @param Wechat $wechat
     * @param $item
     * @return array
     */
    public static function cqRefund(Wechat $wechat,$item)
    {
        //$item=["4002612001201608050626102798","WX","0.01","wx5974803"];

        $orderId = $item[0];
        $deposit = intval($item[2]*100);

        $input = new \WxPayRefund();
//        $wechat = Wechat::findOne($wechatId);
        //证书路径
        self::wechatParams($wechat);
        $input->SetAppid($wechat->appid);
        $input->SetMch_id($wechat->wx_mchid);
        $input->SetOut_trade_no($orderId);
        $input->SetOut_refund_no($orderId);
        $input->SetTotal_fee($deposit);
        $input->SetRefund_fee($deposit);
        $input->SetOp_user_id($wechat->wx_mchid);

        $refund = \WxPayApi::refund($input);
        Yii::getLogger()->log('Weixinpay refund created, response from weixin:' . json_encode($refund),
            Logger::LEVEL_INFO);

        if (isset($refund['result_code']) && $refund['result_code'] === 'SUCCESS') {
            return ['success' => true, 'data' => 'success'];
        }
        return ['success' => false, 'message' => isset($refund['err_code_des']) ? $refund['err_code_des'] : $refund['return_msg']];
    }

}
